//
//  NFSRadioView.m
//  NeedForSpeed
//
//  Created by 王国强 on 15/10/19.
//  Copyright © 2015年 Avenger. All rights reserved.
//

#import "NFSRadioView.h"

#import "BaseHeader.h"

#import "NFSRadioCollectionViewCell.h"

#import "AFNETWORK.h"


@implementation NFSRadioView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame address:(NSString *)address type : (NSString *)type;
{
    self = [super initWithFrame:frame];
    if (self) {
        _radioArray = [[NSMutableArray alloc]init];
        _bigRadioArray =[[NSMutableArray alloc]init];
        [self getData:address];
        [self createCollection:frame];
        self.type = type;

    }
    return self;
}


- (void)getData:(NSString *)address
{
    [AFNETWORK getdataWithString:address block:^(id block) {
        
        for (NSDictionary *dic in [[block objectForKey:@"result"]objectForKey:@"list"] ) {
            NFSRadioModel *model = [[NFSRadioModel alloc]initWithDictionary:dic];
            if (model.type.integerValue != 0) {
                [_radioArray addObject:model];
            }
            [_bigRadioArray addObject:model];
        }
        [self.radioCollectionView reloadData];

    }];
}
- (void)createCollection:(CGRect)fram
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    [layout setItemSize:CGSizeMake(SCREEN_WIDTH / 3  , SCREEN_HEIGHT * 6 / 25)];
    [layout setMinimumInteritemSpacing:0.];
    [layout setMinimumLineSpacing:0.];
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    [layout setSectionInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    
    _radioCollectionView  = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, fram.size.width, fram.size.height) collectionViewLayout:layout];
    _radioCollectionView.delegate =self;
    _radioCollectionView.dataSource =self;
    [_radioCollectionView setBackgroundColor:[UIColor whiteColor]];
    [_radioCollectionView setContentSize:CGSizeMake(0, SCREEN_HEIGHT * 2)];
    [_radioCollectionView setShowsHorizontalScrollIndicator:NO];
    [self addSubview:_radioCollectionView];
    
    [_radioCollectionView registerClass:[NFSRadioCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    

}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if ([self.type isEqualToString:@"1"]) {
        return _radioArray.count;

    }else
    {

        return _bigRadioArray.count;
    }
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NFSRadioCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    if ([self.type isEqualToString:@"1"]) {
        NFSRadioModel *model = [_radioArray objectAtIndex:indexPath.row];
        [cell getdata:model];
    }else
    {
        NFSRadioModel *model = [_bigRadioArray objectAtIndex:indexPath.row];
        [cell getdata:model];
    }

    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.type isEqualToString:@"1"]) {
        NFSRadioModel *model = [_radioArray objectAtIndex:indexPath.row];
        [self.NFSRadioDelegate pushNext:model];
    }else
    {
        NFSRadioModel *model = [_bigRadioArray objectAtIndex:indexPath.row];
        [self.NFSRadioDelegate pushNext:model];
    }

}
@end
